﻿using CalligraphyEditor.CalligraphyData;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace CalligraphyEditor.ViewModel
{
    public class VMRubbingPhotos
    {
        public VMRubbingPhotos()
        { }
        private T_Rubbing _r;

        private CollectionViewSource _cvs;
        public CollectionViewSource Cvs
        {
            get { return _cvs; }
        }

        private ObservableCollection<T_RubbingPhoto> _ocl;

        public ObservableCollection<T_RubbingPhoto> Ocl
        {
            get { return _ocl; }
            set { _ocl = value; }
        }

        public VMRubbingPhotos(T_Rubbing r)
        {
            _r = r;
            var q = from rp in CalligraphyEditor.App.Entities.T_RubbingPhoto.Expand("T_Photo").Expand("T_Rubbing") where rp.RubbingID == r.ID && (rp.IsDeleted == null || rp.IsDeleted == false) select rp;
            _ocl = new ObservableCollection<T_RubbingPhoto>(q);
            _cvs = new CollectionViewSource();
            _cvs.Source = _ocl;

        }

        public void Delete()
        {
            if (_ocl.Count == 0)
                return;
            foreach (var rp in _ocl)
            {
                rp.IsDeleted = true;
                CalligraphyEditor.App.Entities.UpdateObject(rp);

            }
            CalligraphyEditor.App.Entities.SaveChanges();
            _ocl.Clear();
        }

        public string Upload()
        {
            string errorMsg = string.Empty;
            Dictionary<int, string> photoNumsFilePaths = new Dictionary<int, string>();
            Dictionary<int, string> txtNumsFilePaths = new Dictionary<int, string>();
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                goto finish;
            else
            {
                var paths = System.IO.Directory.GetFiles(dialog.SelectedPath);

                string[] photoPaths = PhotoHelper.GetPhotoPaths(paths);
                string[] txtPaths = PhotoHelper.GetTxtPaths(paths);
                errorMsg = PhotoHelper.GetErrorMsg(paths, photoPaths, txtPaths);
                if (!string.IsNullOrEmpty(errorMsg))
                //var photoPaths = System.IO.Directory.GetFiles(dialog.SelectedPath, "*.jpg", System.IO.SearchOption.TopDirectoryOnly);
                //var txtPaths = System.IO.Directory.GetFiles(dialog.SelectedPath, "*.txt", System.IO.SearchOption.TopDirectoryOnly);
                {
                    goto finish;

                }
                else
                {
                    photoNumsFilePaths = PhotoHelper.FilePathsToNums(photoPaths);
                    txtNumsFilePaths = PhotoHelper.FilePathsToNums(txtPaths);
                    for (int i = 1; i <= photoNumsFilePaths.Count; i++)
                    {
                        var photo = PhotoHelper.UploadPhoto(photoNumsFilePaths[i]);
                        T_RubbingPhoto rp = new T_RubbingPhoto();
                        rp.T_Photo = photo;
                        rp.PhotoID = photo.ID;
                        rp.PageNumber = i;
                        rp.PhotoPath = PhotoHelper.GetPath(i, photoNumsFilePaths);
                        rp.RubbingID = this._r.ID;
                        rp.T_Rubbing = _r;
                        rp.Text = PhotoHelper.GetText(i, txtNumsFilePaths);
                        //_r.T_RubbingPhoto.Add(rp);
                        photo.T_RubbingPhoto.Add(rp);
                        //CalligraphyEditor.App.Entities.AddToT_RubbingPhoto(rp);
                        //CalligraphyEditor.App.Entities.SaveChanges();
                        _ocl.Add(rp);
                        _r.T_RubbingPhoto.Add(rp);
                    }
                    ShowPhotoes sp = new ShowPhotoes(_ocl);
                    sp.Show();

                }
            }

        finish:
            return errorMsg;
        }
        //public void UplocdPhoto(T_RubbingPhoto rp)
        //{
        //    _r.T_RubbingPhoto.Add(rp);
        //    //photo.T_RubbingPhoto.Add(rp);
        //    CalligraphyEditor.App.Entities.AddToT_RubbingPhoto(rp);
        //    CalligraphyEditor.App.Entities.SaveChanges();

        //}


        internal void UploadPhoto(T_RubbingPhoto t_RubbingPhoto, int i, int count, System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs e)
        {
            //worker.ReportProgress(count + i);
            int j = 0;
            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                j = 1;
                worker.ReportProgress(j);
                //_r.T_RubbingPhoto.Add(t_RubbingPhoto);
                j = 2;
                worker.ReportProgress(j);
                //photo.T_RubbingPhoto.Add(rp);
                CalligraphyEditor.App.Entities.AddToT_RubbingPhoto(t_RubbingPhoto);
                try
                {
                    CalligraphyEditor.App.Entities.SaveChanges();
                    j = 3;
                    worker.ReportProgress(j);
                    worker.ReportProgress(count + i);
                }
                catch (DataServiceRequestException)
                {
                    //MessageBox.Show("第" + (i + 1).ToString() + "张图片上传失败", "上传失败", MessageBoxButton.OKCancel);
                    var dialog = MessageBox.Show( "上传失败,是否继续上传", "上传失败", MessageBoxButton.OKCancel);
                    if (dialog.Equals(System.Windows.MessageBoxResult.Cancel))
                    {
                        worker.CancelAsync();
                        //e.Cancel = true;
                    }
                }
            }
        }


        internal void Save()
        {
            if (_cvs.View.CurrentItem == null)
                return;
            CalligraphyEditor.App.Entities.UpdateObject(_cvs.View.CurrentItem);
            CalligraphyEditor.App.Entities.SaveChanges();
        }
    }
}
